import whisper
from flask import Flask, request, jsonify
import os


app = Flask(__name__)

# 设置上传文件的保存目录
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/')
def hello_world():
    return 'Hello, World! This is a file upload API.'

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({'error': 'No file part'})

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'No selected file'})

    if file:
        filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
        file.save(filename)
        result = model.transcribe(filename)
        return jsonify({'message': result})

@app.route('/api', methods=['POST'])
def process_data():
    data = request.json  # 获取POST请求的JSON数据
    result = {'message': 'Data received:', 'data': data}
    return jsonify(result)

if __name__ == '__main__':
    model = whisper.load_model("small.en")
    app.run(host='0.0.0.0', port=5000)
